package cn.enilu.guns.admin.modular.door;

import cn.enilu.guns.admin.common.constant.device.ParamConstant;
import cn.enilu.guns.admin.core.base.controller.BaseController;
import cn.enilu.guns.admin.core.base.tips.SetDataToDeviceTip;
import cn.enilu.guns.admin.core.util.EntranceDataUtil;
import cn.enilu.guns.admin.equipment.dto.DeviceInfo;
import cn.enilu.guns.admin.equipment.service.EntranceGuardService;
import cn.enilu.guns.bean.entity.door.EquipmentInfo;
import cn.enilu.guns.bean.vo.query.SearchFilter;
import cn.enilu.guns.service.door.EquipmentInfoService;
import cn.enilu.guns.utils.BeanUtil;
import cn.enilu.guns.utils.IpUtils;
import cn.enilu.guns.utils.Lists;
import cn.enilu.guns.warpper.door.DeviceInfoWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/door/device/mgr")
public class DeviceMgrController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private EntranceGuardService entranceGuardService;
	@Autowired
	private EquipmentInfoService equipmentInfoService;

	private static String PREFIX = "/door/device/";

	/**
	* 跳转到首页
	*/
	@RequestMapping(value="",method = RequestMethod.GET)
		public String index() {
		return PREFIX + "index.html";
	}

	/**
	* 跳转到添加页面
	*/
	@RequestMapping(value = "/add",method = RequestMethod.GET)
		public String add() {
		return PREFIX + "add.html";
	}

	/**
	* 跳转到修改页面
	*/
	@RequestMapping("/edit")
	public String edit(DeviceInfo deviceInfo, Model model) {
		model.addAttribute("deviceInfo",deviceInfo);
		String str = entranceGuardService.getIp(deviceInfo.getIpAddress());
		Map<String,String> map = EntranceDataUtil.parseParams(str);
		deviceInfo.setGateipAddress(map.get(ParamConstant.GATE_IP_ADDRESS));
		deviceInfo.setNetMask(map.get(ParamConstant.NET_MASK));
		return PREFIX + "edit.html";
	}

	//搜索设备，返回list
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	@ResponseBody
	public Object list(@RequestParam(required = false) String equipmentName) {
        List<DeviceInfo> deviceInfos = entranceGuardService.searchDevice();
        if(deviceInfos == null){
        	return null;
		}
		for (DeviceInfo deviceInfo:deviceInfos) {
			List<SearchFilter> searchFilters = Lists.newArrayList();
			searchFilters.add(new SearchFilter("serialNumber",SearchFilter.Operator.EQ,deviceInfo.getSerisalNumber()));
			List<EquipmentInfo> equipmentInfos = equipmentInfoService.queryAll(searchFilters);
			if(equipmentInfos.size() >0){
				deviceInfo.setYnAdd("1");
				deviceInfo.setCommunicationMode(equipmentInfos.get(0).getCommunicationMode());
			}else{
				deviceInfo.setYnAdd("0");
			}
		}
		return super.warpObject(new DeviceInfoWarpper(BeanUtil.objectsToMaps(deviceInfos)));
	}

	/**
	* 修改设备IP
	*/
	@RequestMapping(value = "/updateIp")
	@ResponseBody
	public Object updateIp(DeviceInfo deviceInfo) {
		//检查IP是否正确
		if(!IpUtils.isRightIP(deviceInfo.getNewIpAddress())){
			return IPERROR_TIP;
		}
		//检查IP是否正确
		if(!IpUtils.isRightIP(deviceInfo.getNewGateipAddress())){
			SetDataToDeviceTip setDataToDeviceTip = new SetDataToDeviceTip(202,"网关输入有误");
			return setDataToDeviceTip;
		}
		//检查IP是否正确
		if(!IpUtils.isRightIP(deviceInfo.getNewNetMask())){
			SetDataToDeviceTip setDataToDeviceTip = new SetDataToDeviceTip(202,"子网掩码输入有误");
			return setDataToDeviceTip;
		}
		String rs = entranceGuardService.setIpAddress(deviceInfo);

        SetDataToDeviceTip setDataToDeviceTip = new SetDataToDeviceTip(200,rs);
        return setDataToDeviceTip;
	}

}